'''
Created on 16.08.2011

@author: osman
'''

import opf
from pets_ontology import Pets, Pet, Cat
from people_ontology import People, Person

@Pets
class Snake(Pet):
    @opf.attribute
    def length(self, val): hasLength(self, val)

@Pets.atomic
def hasLength(snake: Snake, val: int) -> 'has length':
    return val > 0

@Pets
class BetterSnake(Snake):
    @opf.attribute
    def length(self, val):
        opf.context().refute(predicate = hasLength, snake = self)
        hasLength(self, val)

@People.atomic
def isCloseRelativeOf(person1: Person, person2: Person) -> 'is close relative of':
    return True

@Pets.atomic
def hasOwner(pet: Pet, owner: Person) -> 'has owner':
    return True

@Pets
def hasCoOwner(pet: Pet, owner: opf.exists(Person), coOwner: Person) -> 'has co-owner':
    hasOwner(pet, owner), isCloseRelativeOf(owner, coOwner)

family = opf.Ontology()
with family:
    Tiger = Cat("Tiger")
    Oscar = Cat("Oscar")
    Robert = Person("Robert")
    Mary = Person("Mary")
    hasOwner(Tiger, Robert)
    isCloseRelativeOf(Robert, Mary)

print(family.count(predicate = hasCoOwner))
for x in family.find(predicate = hasCoOwner):
    print(x)
print(family.match(predicate = hasCoOwner, pet = Tiger, coOwner = Mary))
print(family.match(predicate = hasCoOwner, pet = Oscar, coOwner = Mary))

